You need to close the file as soon as the write is complete. If the unit shuts down while the file is open there's a good chance to corrupt the flash. This will erase the flash when it reboots.
@jmarcelino Thank you for sharing this. To add to this question, are there code example of we can persist an array of structs to the file system? I have a clear idea of how I want to implement a simple persistent database but I cant find micropython examples of doing so. The btree library would have been great but I realised I couldnt use it in this version of the port.